using System;
using System.Collections.Generic;
using System.Text;

namespace cb0tServer
{
    partial class ServerCore
    {
        private void OnFileReceived(UserObject userobj, SharedFileObject file)
        {
            FilterItem[] items = FileFilters.GetFilters();

            if (items == null) return;

            foreach (FilterItem filter in items)
            {
                if (userobj.logged_in) // still connected?
                {
                    if (file.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                    {
                        switch (filter.type)
                        {
                            case "kill":
                                if (userobj.admin || userobj.host) break;
                                if (file.type == 1) break; // never kill audio files
                                UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(43).Replace("+n", userobj.name).Replace("+f", file.name)));
                                userobj.Disconnect();
                                this.OnPart(userobj);
                                Stats.UsersParted++;
                                Stats.KickedUsers++;
                                AdminLog.WriteLine(userobj.name + " was kicked for sharing " + file.name);
                                return;

                            case "ban":
                                if (userobj.admin || userobj.host) break;
                                if (file.type == 1) break; // never ban audio files
                                UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(44).Replace("+n", userobj.name).Replace("+f", file.name)));
                                this.banstats.Add(userobj.name + " [" + userobj.physical_ip.ToString() + "] was banned by the file filter");
                                Bans.AddBan(userobj);
                                userobj.Disconnect();
                                this.OnPart(userobj);
                                Stats.UsersParted++;
                                Stats.BannedUsers++;
                                Stats.KickedUsers++;
                                AdminLog.WriteLine(userobj.name + " was banned for sharing " + file.name);
                                return;

                            case "pm":
                                userobj.SendOutboundPacket(ServerOutboundPackets.PMPacket(CoreSettings.room_bot,
                                    filter.args.Replace("+f", file.name).Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                                break;

                            case "announce":
                                if (userobj.files_checked++ > 10) break;
                                UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.AnnoucePacket(filter.args.Replace("+f",
                                    file.name).Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                                break;

                            case "clone":
                                if (userobj.files_checked++ > 10) break;
                                UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.PublicTextPacket(userobj.name, filter.args.Replace("+f",
                                    file.name).Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                                break;

                            default:
                                break;
                        }
                    }
                }
                else
                {
                    return;
                }
            }
        }
    }
}
